package kr.hkit.password.fragment.account;

import kr.hkit.password.R;
import kr.hkit.password.dao.InfoDao;
import kr.hkit.password.db.MyDatabaseManager;
import android.app.Fragment;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class AccountSearch extends Fragment {
	private static final String TAG = "PW";
	private EditText eAccount;
	private EditText eAnswer;
	private int hintPosition;
	private TextView tvResult;
	private MyDatabaseManager mAdb;
	private ArrayAdapter<CharSequence> adspin;
	private boolean isSelect;
	private Spinner spin;
	private Button btn;
	private InfoDao info;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.d(TAG, "AccountSearch onCreateView");
		View view = inflater.inflate(R.layout.account_search, container, false);
		info = (InfoDao) getActivity().getApplication();
		info.setMain(true);
		eAccount = (EditText) view.findViewById(R.id.account);
		eAnswer = (EditText) view.findViewById(R.id.answer);
		tvResult = (TextView) view.findViewById(R.id.result);
		btn = (Button) view.findViewById(R.id.btnSearch);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Log.d(TAG, "AccountSearch onClick");
				if (isSelect) {
					String[] args = { eAccount.getText().toString(),
							hintPosition + "", eAnswer.getText().toString() };
					mAdb = MyDatabaseManager.getInstance(getActivity());
					Cursor rs = mAdb.select("account",
							new String[] { "password" },
							"account=? and hint=? and answer=?", args, null,
							null, null);
					if (rs.moveToNext()) {
						Log.d(TAG, "AccountSearch moveToNext" + rs.getString(0));
						tvResult.setText(rs.getString(0));
					}
					rs.close();
				} else {
					Toast.makeText(getActivity(), "힌트를 선택하세요.",
							Toast.LENGTH_LONG).show();
				}
			}
		});

		spin = (Spinner) view.findViewById(R.id.spinner);
		spin.setPrompt("힌트를 선택하세요.");
		adspin = ArrayAdapter.createFromResource(getActivity(), R.array.hint,
				android.R.layout.simple_spinner_item);
		adspin.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spin.setAdapter(adspin);

		spin.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				isSelect = true;
				hintPosition = position;
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
			}

		});
		return view;
	}
}
